/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pesancharter.operator;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author fendy
 */
@WebServlet(name="PesanCharterServlet", urlPatterns="/PesanCharterServlet")
public class PesanCharterServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //response.sendRedirect("OrderChM.jsp");
    }

    protected void processSubmit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try{
            out.println(request.getParameter("textfield3"));
            out.println(request.getParameter("jumpMenu2"));
            out.println(request.getParameter("jumpMenu3"));
            out.println(request.getParameter("jumpMenu4"));
            out.println(request.getParameter("jumpMenu"));
            out.println(request.getParameter("radio"));
            out.println(request.getParameter("sewa"));
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
        RequestDispatcher dispatcher =  getServletContext().getRequestDispatcher("/OrderChM_01.jsp");
        dispatcher.forward(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processSubmit(request, response);

        String command = request.getParameter("command");
        System.out.println(command);

        if (command == null)
        {
            RequestDispatcher dispatcher =  getServletContext().getRequestDispatcher("/OrderChM_01.jsp");
            dispatcher.forward(request, response);
        }
        System.out.println(command);

        if (command.equals("1"))
        {
            System.out.println(command);
            int tgl = Integer.parseInt(request.getParameter("jumpMenu2"));
            int bulan = Integer.parseInt(request.getParameter("jumpMenu3"));
            int tahun = Integer.parseInt(request.getParameter("jumpMenu4"))-1900;
            int total_hari = Integer.parseInt(request.getParameter("sewa"));

            System.out.println(command);
            
            ArrayList<Integer> availMobil = new ArrayList<Integer>();
            Date date = new Date(tahun, bulan, tgl);
            try {
                availMobil = PesanCharterEntity.GetAvailableMobil(date);

                //next day, dst
                for (int i=1; i<total_hari; i++)
                {
                    date = new Date(date.getTime()+86400000);
                    ArrayList<Integer> availMobil2 = PesanCharterEntity.GetAvailableMobil(date);
                    if (availMobil == null || availMobil2 == null)
                    {
                        availMobil = null;
                        break;
                    }
                    availMobil.retainAll(availMobil2);
                }

                
            }
            catch (SQLException e)
            { }

            int id_supir_tersedia = -1;
            date = new Date(tahun, bulan, tgl);
            try {
                ArrayList<Integer> availSupir = PesanCharterEntity.GetAllAvailableSupir(date);

                //next day, dst
                for (int i=1; i<total_hari; i++)
                {
                    date = new Date(date.getTime()+86400000);
                    ArrayList<Integer> availSupir2 = PesanCharterEntity.GetAllAvailableSupir(date);
                    if (availSupir == null || availSupir2 == null)
                    {
                        availSupir = null;
                        break;
                    }
                    availSupir.retainAll(availSupir2);
                }
                if (availSupir != null && availSupir.size() > 0)
                {
                    id_supir_tersedia = availSupir.get(0);
                }
            }
            catch (SQLException e)
            { }
            
            ArrayList<String> availMobilName = PesanCharterEntity.GetJenisMobil(availMobil);

            System.out.println(new Date(tahun, bulan, tgl));
            System.out.println(total_hari);
            System.out.println(availMobil);
            System.out.println(availMobilName);
            
            request.setAttribute("tanggal_berangkat", new Date(tahun, bulan, tgl));
            request.setAttribute("total_hari", total_hari);
            request.setAttribute("id_supir_tersedia", id_supir_tersedia);
            request.setAttribute("mobil_tersedia_id", availMobil);
            request.setAttribute("mobil_tersedia_name", availMobilName);

            CallPage(request, response);
        }
        else if (command.equals("2"))
        {
            System.out.println(command);
            int tgl = Integer.parseInt(request.getParameter("tanggal"));
            int bulan = Integer.parseInt(request.getParameter("bulan"));
            int tahun = Integer.parseInt(request.getParameter("tahun"));
            int total_hari = Integer.parseInt(request.getParameter("sewa"));
            String nama_pemesan = request.getParameter("textfield3");
            int id_mobil = Integer.parseInt(request.getParameter("jumpMenu"));
            int id_supir = Integer.parseInt(request.getParameter("id_supir"));
            String status_supir = request.getParameter("radio");
            if (status_supir.equals("2")) // tak pake supir
            {
                id_supir = -1;
            }
            //// id user operator sementara, nanti dari session
            int id_user = 0;
            ///

            
            try {
                PesanCharterEntity.InsertPesanCharter(id_user, nama_pemesan, id_mobil, new Date(tahun, bulan, tgl), id_supir, 0, "", total_hari);
            } catch (SQLException ex) {
                Logger.getLogger(PesanCharterServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

            response.sendRedirect("/SMARTravel-war/");
        }
        
    }



    private void CallPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        /*ArrayList<DBPkt> dpktList = DBPkt.getAllRows();

        if (dpktList != null)
        {
            request.setAttribute("dpktList", dpktList);
        }
         *
         */
        
        RequestDispatcher dispatcher =  getServletContext().getRequestDispatcher("/OrderChM_02.jsp");
        dispatcher.forward(request, response);
    }



    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
